package org.zht.dormpro.dorm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.cacher.IFloorCacher;
import org.zht.dormpro.dorm.mapper.TbFloorMapper;
import org.zht.dormpro.dorm.model.TbFloor;
import org.zht.dormpro.dorm.service.IFloorService;

import java.util.Collections;
import java.util.List;

@Service
public class FloorServiceImpl implements IFloorService{

    @Autowired
    private TbFloorMapper floorMapper;
    @Autowired
    private IFloorCacher floorCacher;

    @Override
    public List<TbFloor> getFloorByCondition(PageBean pageBean) {
        Page<Object> objects = PageHelper.startPage(pageBean.getPageNum(), pageBean.getLimit());
        List<TbFloor> tbFloors = floorMapper.getAllFloorByCondition(pageBean);
        pageBean.setTotal((int) objects.getTotal());
        return tbFloors;
    }

    @Override
    public TbFloor getFloorById(Integer id) {
        return floorMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean editFloor(TbFloor floor) {
        int i = floorMapper.updateByPrimaryKeySelective(floor);
        return i > 0;
    }

    @Override
    public boolean addFloor(TbFloor floor) {
        int i = floorMapper.insertSelective(floor);
        return i > 0;
    }

    @Override
    public boolean deleteFloor(Integer id) {
        int i = floorMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public boolean batchRemoveFloor(List<String> floorIds) {
        if(CollectionUtil.isNotEmpty(floorIds)){
            for (String floorId: floorIds) {
                boolean b = deleteFloor(Integer.valueOf(floorId));
                if (!b){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<TbFloor> getAllFloor(int all) {
        return floorMapper.getAllFloor(all);
    }
    @Override
    public List<TbFloor> getAllFloor() {
        //使用缓存获取有用的宿舍楼
        return floorCacher.getAllFloor();
    }

}
